home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / utils / pltek.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-27  |  5.9 KB  |  247 lines

  1. /* $Id: pltek.c,v 1.7 1994/08/26 19:25:40 mjl Exp $
  2.  * $Log: pltek.c,v $
  3.  * Revision 1.7  1994/08/26  19:25:40  mjl
  4.  * Now checks for the terminal type and provides some rudimentary decisions
  5.  * based on the TERM setting.  The xterm is unaffected, but with a terminal
  6.  * type of "tekterm", the 'Page >' prompt is printed on the graphics screen
  7.  * so that the plot can be seen without having pltek flash back to the text
  8.  * screen immediately.  Other minor cleaning up as well.  Contributed by Mark
  9.  * Olesen.
  10.  *
  11.  * Revision 1.6  1994/08/10  01:15:11  mjl
  12.  * Changed/improved page prompt, enabled both forward and backward relative
  13.  * seeking, eliminated system dependent input code.
  14.  *
  15.  * Revision 1.5  1994/06/30  18:55:52  mjl
  16.  * Minor changes to eliminate gcc -Wall warnings.
  17. */
  18.  
  19. /*
  20.  *  pltek.c
  21.  *  Review a Tektronix vector file.
  22.  *  from 'scan', by Clair Nielsen, LANL.
  23.  *  Modifications by Maurice LeBrun, IFS.
  24.  *
  25.  *  This version works well with xterm and at least one vt100/tek emulator
  26.  *  I've tried.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <math.h>
  32. #include <ctype.h>
  33.  
  34. static void describe(void);
  35. static void tek_mode(int mode);
  36.  
  37. #define ESC  27                /* ESCape character */
  38.  
  39. #define ALPHA_MODE    0        /* switch to alpha mode */
  40. #define GRAPH_MODE    1         /* switch to graph mode */
  41. #define BUFSZ    1024
  42. #define MAXPAGES 1024
  43.  
  44. enum { unknown, xterm, tekterm } termtype = unknown;
  45.  
  46.  
  47. int
  48. main(int argc, char *argv[])
  49. {
  50.     FILE *fd;
  51.     int i, j, nb, npage, ipage, nextpage, ifirst, oldpage;
  52.     int istop;
  53.     long start[MAXPAGES];        /* start (offset) of each page */
  54.     char buf[BUFSZ], xtra, lastchar;
  55.     char c, ibuf[128], *t;
  56.  
  57.     if (argc < 2) {
  58.     describe();
  59.     exit(1);
  60.     }
  61.     if ((fd = fopen(argv[1], "r")) == NULL) {
  62.     printf("Cannot open %s\n", argv[1]);
  63.     exit(1);
  64.     }
  65.    
  66.     if ( (t = getenv("TERM")) != NULL ) {      
  67.     if ( strcmp("xterm", t) == 0 ) { 
  68.         termtype = xterm;     
  69.     } else if (!strncmp("tek",t,3) ||
  70.            !strncmp("401",t,3) ||
  71.            !strncmp("410",t,3) ) {
  72.         termtype = tekterm;
  73.     }
  74.     }
  75.  
  76. /* Find out how many pages there are in file. */
  77.  
  78.     ipage = 0;
  79.     start[0] = 0;
  80.     for (i = 0; i < MAXPAGES; i++) {
  81.     nb = fread(buf, 1, BUFSZ, fd);
  82.     if (nb <= 0)
  83.         break;
  84.     ifirst = 0;
  85.     for (j = 0; j < nb; j++) {
  86.         if ((lastchar = buf[j]) == '\f') {
  87.         ifirst = j - 1;
  88.         ipage++;
  89.         start[ipage] = BUFSZ * i + ifirst + 2;
  90.         }
  91.     }
  92.     }
  93.  
  94. /* don't count a FF at the end of the file as a separate page */
  95.  
  96.     if (lastchar == '\f')
  97.     ipage--;
  98.  
  99.     npage = ipage + 1;
  100.  
  101. /* Loop until the user quits */
  102.  
  103.     ipage = 0;
  104.     while (1) {
  105.     oldpage = ipage;
  106.     printf("Page %d/%d> ", ipage, npage);
  107.  
  108.     gets(ibuf);
  109.     c = ibuf[0];
  110.  
  111. /* User input a page number or a return */
  112. /* A carriage return in response to the prompt proceeds to the next page. */
  113.  
  114.     if (c == '\0') {         /* CR = increment by one page */
  115.         ipage++;
  116.     } else if (c == '+') {        /* +<n> = increment by <n> pages */
  117.         ipage += atoi(ibuf+1);
  118.     } else if (c == '-') {        /* -<n> = decrement by <n> pages */
  119.         ipage -= atoi(ibuf+1);
  120.     } else if (isdigit(c)) {    /* <n> = goto page <n> */
  121.         ipage = atoi(ibuf);
  122.     } else if (c == '\f') {
  123.         ;                /* FF = redraw the last plot */
  124.     } else if (c == 'q') {        /* q = quit */
  125.         break;
  126.     } else {            /* h, ? or garbage = give help */
  127.         tek_mode(ALPHA_MODE);
  128.         describe();    
  129.         continue;
  130.     }
  131.  
  132. /* Bounds checking */
  133.  
  134.     if (ipage > npage) {
  135.         ipage = npage;
  136.         if (ipage == oldpage)
  137.         continue;
  138.     } else if (ipage < 0) {
  139.         ipage = 1;
  140.     } else if (ipage == 0) {
  141.         continue;            /* page 0 does not exist */
  142.     }
  143.  
  144. /* Time to actually plot something. */
  145.  
  146.     tek_mode(GRAPH_MODE);
  147.     istop = fseek(fd, start[ipage - 1], 0);
  148.     xtra = '\0';
  149.     istop = 0;
  150.     for (i = 0; i < 8196; i++) {    /* less than 8MB per page! */
  151.         if (xtra) {
  152.         fwrite(&xtra, 1, 1, stdout);
  153.         xtra = '\0';
  154.         }
  155.         nb = fread(buf, 1, BUFSZ, fd);
  156.         if (nb <= 0)
  157.         break;
  158.         ifirst = 0;
  159.         for (j = 0; j < nb; j++) {
  160.         if (buf[j] == '\f') {
  161.             fwrite(&buf[ifirst], 1, j - ifirst, stdout);
  162.             fflush(stdout);
  163.             istop = 1;
  164.             break;
  165.         }
  166.         }
  167.         if (istop)
  168.         break;
  169.         if (buf[nb] == ESC) {
  170.         xtra = ESC;
  171.         j--;
  172.         }
  173.         fwrite(&buf[ifirst], 1, j - ifirst, stdout);
  174.     }
  175.     if ( termtype == xterm ) 
  176.         tek_mode(ALPHA_MODE);
  177.     }
  178.     tek_mode(ALPHA_MODE);        /* how to kill an xterm Tek window? */
  179.     fclose(fd);
  180.     exit(0);
  181. }
  182.  
  183. /*----------------------------------------------------------------------*\
  184.  *  tek_mode()
  185.  *
  186.  *  switch to alpha/graph mode
  187. \*----------------------------------------------------------------------*/
  188.  
  189. static int currentmode = ALPHA_MODE;
  190. static void 
  191. tek_mode (int mode)
  192. {
  193.     if ( mode == GRAPH_MODE ) {
  194.     if ( currentmode == ALPHA_MODE ) {
  195.         switch(termtype) {
  196.         case tekterm:
  197.         break;
  198.         case xterm:
  199.         printf("\033[?38h");        /* open graphics window */
  200.         break;
  201.         default:
  202.         printf("\033[?38h");        /* open graphics window */
  203.         }
  204.         printf("\035");            /* Enter Vector mode = GS */
  205.     }
  206.     printf("\033\f");            /* clear screen = ESC FF */
  207.     fflush(stdout);      
  208.     currentmode == mode;
  209.     } else if ( mode == ALPHA_MODE ) {
  210.     switch(termtype) {
  211.     case tekterm:
  212.         printf("\037\030");        /* Enter Alpha mode = US CAN */
  213.         break;
  214.     case xterm:
  215.         printf("\033\003");        /* VT mode (xterm) = ESC ETX */
  216.         break;
  217.     default:
  218.         printf("\033\003");        /* VT mode (xterm) = ESC ETX */
  219.     }
  220.     fflush(stdout);      
  221.     currentmode == mode;
  222.     }
  223. }
  224.  
  225. /*----------------------------------------------------------------------*\
  226.  *  describe()
  227.  *
  228.  *  Print help message.
  229.  *  Note: if this message starts to exceed 512 bytes, may need to split
  230.  *  since some compilers can't handle strings that long.
  231. \*----------------------------------------------------------------------*/
  232.  
  233. static void 
  234. describe (void)
  235. {
  236.     fputs("\
  237. Usage: pltek filename \n\
  238. At the prompt, the following replies are recognized:\n\
  239.    h,?      Give this help message.\n\
  240.     q      Quit program.\n\
  241.    <n>      Go to the specified page number.\n\
  242.    -<n>   Go back <n> pages.\n\
  243.    +<n>   Go forward <n> pages.\n\
  244.  <Return> Go to the next page.\n\n",
  245.       stdout);
  246. }
  247.